// 示例方法，没有实际意义
export function trim(str: string) {
  return str.trim();
}

// 数据分组
export function groupOrders({ orders, timeKey, orderNameKey, orderCodeKey }) {
  const newList = orders.map((item) => {
    const orderList: any = [];
    if (item[orderNameKey].length === item[orderCodeKey].length) {
      item.orderName.forEach((n, i) => {
        const code = item[orderCodeKey][i];
        orderList.push([n, code]);
      });
    }
    const time = new Date(item[timeKey]);
    const date = `${time.getFullYear()}-${
      time.getMonth() + 1
    }-${time.getDate()}`;
    return {
      date,
      orderList,
    };
  });

  return newList.reduce((acc, order) => {
    const date = order.date;
    order.orderList.forEach(([itemName, itemCode]) => {
      if (!acc[date]) {
        acc[date] = {};
      }
      if (!acc[date][itemName]) {
        acc[date][itemName] = [];
      }
      acc[date][itemName].push(itemCode);
    });
    return acc;
  }, {});
}

// 复制
export async function copyToClipboard(text) {
  try {
    await navigator.clipboard.writeText(text);
    console.log('Text copied to clipboard');
  } catch (err) {
    console.error('Failed to copy text: ', err);
  }
}
